package service;

import dao.UserDao;
import model.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserService {
    private UserDao uDao = new UserDao();

    public UserService() {
    }

    public boolean register(User users) {
        try {
            if (this.uDao.isUsernameExist(users.getAccount())) {
                return false;
            } else if (this.uDao.isEmailExist(users.getEmail())) {
                return false;
            } else if (this.uDao.isPhone(users.getPhone())) {
                return false;
            } else {
                this.uDao.addUser(users);
                return true;
            }

        } catch (SQLException var3) {
            var3.printStackTrace();
            return true;
        }
    }

    public User Login(String account, String password) {
        User user = null;
        try {
            user = uDao.selectByUsernamePassword(account, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user != null) {
            return user;
        }
        try {
            user = uDao.selectByEmailPassword(account, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user != null) {
            return user;
        }
        return null;
    }



    public boolean delete(int id) {
        try {
            uDao.delete(id);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<User> getUserList() {
        List<User> userLis = new ArrayList<>();
        try {
            userLis = uDao.getAllUser();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (userLis != null) {
            return userLis;
        }
        return null;
    }

    public User selectById(int id) {
        User u = null;
        try {
            u = uDao.selectById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;
    }
    public void updateUserAddress(User users) {
        try {
            uDao.updateUserAddress(users);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

